package top.aigoo.project01.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.aigoo.project01.entity.ProductCategory;
import top.aigoo.project01.entity.dto.ProductCategoryExecution;
import top.aigoo.project01.enums.ProductCategoryEnum;
import top.aigoo.project01.mapper.ProductCategoryMapper;
import top.aigoo.project01.mapper.ProductMapper;
import top.aigoo.project01.service.ProductCategoryService;

import java.util.List;

/**
 * @Description: 商品分类
 * @Author: Mr.OYSHUO.欧阳大叔  mail:1642302522@qq.com
 * @Version 1.0.0
 * @Date: 2021/8/25 22:24
 */

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private ProductMapper productMapper;


    @Override
    public ProductCategoryExecution queryByShopId(long shopId) {
        return new ProductCategoryExecution(ProductCategoryEnum.SUCCESS, productCategoryMapper.queryByShopId(shopId));
    }

    /**
     * 批量添加商品分类
     */
    @Override
    @Transactional
    public ProductCategoryExecution batchAddProductCategory(List<ProductCategory> productCategoryList)
            throws RuntimeException {

        // 初始化
        if (productCategoryList == null || productCategoryList.size() <= 0) {
            return new ProductCategoryExecution(ProductCategoryEnum.INNER_ERROR);
        }
        try {
            int effectNums = productCategoryMapper.bantchInsertProductCategory(productCategoryList);
            if (effectNums <= 0) {
                throw new RuntimeException("店铺类别失败");
            } else {
                return new ProductCategoryExecution(ProductCategoryEnum.SUCCESS);
            }
        } catch (Exception e) {
            throw new RuntimeException("batchAddProductCategory error: " + e.getMessage());
        }
    }

    /**
     * 删除类别
     */
    @Override
    @Transactional
    public ProductCategoryExecution deleteProductCategory(long productCategoryId,
                                                          long shopId)
            throws RuntimeException {
        //先解除tb_product里面的商品与该productCategoryId的关联
        try {
            int effectedNum = productMapper.updateProductCategoryToNull(productCategoryId);
            if (effectedNum <= 0) {
                throw new RuntimeException("商品类别更新失败!");
            }
        } catch (Exception e) {
            throw new RuntimeException("delete productCategory error：" + e.getMessage());
        }
        //删除该商品类别productCategory
        try {
            int effectedNum = productCategoryMapper.deleteProductCategory(
                    productCategoryId, shopId);
            if (effectedNum <= 0) {
                throw new RuntimeException("店铺类别删除失败");
            } else {
                return new ProductCategoryExecution(ProductCategoryEnum.SUCCESS);
            }
        } catch (Exception e) {
            throw new RuntimeException("deleteProductCategory error: "
                    + e.getMessage());
        }
    }

    /**
     * 查询指定某个店铺下的所有商品类别信息
     *
     * @param shopId
     * @return List<ProductCategory>
     */
    @Override
    public List<ProductCategory> getByShopId(long shopId) {
        return productCategoryMapper.queryByShopId(shopId);
    }
}
